<?php
class item_downloadfile extends template_item
{
	var $default_name = "Download a File";
	var $default_description = "When the item is used, an download popup will appear. Then the item will be deleted.";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 0;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['linked_id'] = 0;
		}
	}
	
	function admin_form()
	{
		$items = array();
		$items[] = array(0,"-Use Uploaded File-");
		foreach($this->ipsclass->cache['market_items'] as $item)
		{
			if($item['filename'] == $this->item['filename'] && $item['item_id'] != $this->item['item_id'] && $this->ipsclass->market_class->items_loaded[$item['item_id']]->item_config['linked_id'] == 0)
			{
				$items[] = array($item['item_id'],"Item: ".$item['item_name'].", File:".$this->ipsclass->market_class->items_loaded[$item['item_id']]->item_config['filename']);
			}
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What file to download?</b>".($this->ipsclass->input['do'] == 'edit' ? "<br />If you upload a file, the old one will be deleted. You are not required to upload a file." : ""),
															$this->ipsclass->adskin->form_upload(),
													)	);
		
		if($this->item['item_id'] != 0 && $this->item_config['linked_id'] == 0)
		{
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<b>Current Uploaded File information</b>",
																"File name: ".$this->item_config['filename']."<br />File size: ".$this->item_config['filesize']." Bytes<br />File Location: ".$this->item_config['location'],
														)	);
		}
		
		if(count($items) > 1)
		{
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<b>- OR - Use a previous file uploaded by another item?</b>",
																$this->ipsclass->adskin->form_dropdown("item[".$this->item['item_id']."][linked_id]",$items,$this->item_config['linked_id']),
														)	);
		}
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		require_once(KERNEL_PATH."class_upload.php");
		$upload = new class_upload();
		$upload->ipsclass = &$this->ipsclass;
		
		$save = array();
		if($input['linked_id'] == 0)
		{
			if($this->item['item_id'] == 0 && ($_FILES['FILE_UPLOAD']['name'] == "" or ! $_FILES['FILE_UPLOAD']['name'] or ($_FILES['FILE_UPLOAD']['name'] == "none")))
			{
				$this->ipsclass->admin->error("You need to upload a file first");
			}
			
			$id = 0;
			if($this->item['item_id'] != 0)
			{
				$id = $this->item['item_id'];
			}
			else
			{
				foreach(array_keys($this->ipsclass->cache['market_items']) as $item_id)
				{
					if($item_id > $id)
					{
						$id = $item_id;
					}
				}
				$id++;
			}
			
			if($_FILES['FILE_UPLOAD']['name'] == "" or ! $_FILES['FILE_UPLOAD']['name'] or ($_FILES['FILE_UPLOAD']['name'] == "none"))
			{
				$save = $this->item_config;
			}
			else
			{
				if($this->item_config['location'] && file_exists($this->item_config['location']))
				{
					@unlink( $this->item_config['location'] );
				}
				
				$upload->out_file_name = 'market-download-'.$id;
				$upload->out_file_dir = $this->ipsclass->vars['upload_dir'];
				$upload->upload_form_field = 'FILE_UPLOAD';
				$upload->allowed_file_ext = array($upload->_get_file_extension($_FILES['FILE_UPLOAD']['name']));
				$upload->upload_process();
				
				if($upload->error_no)
				{
					$this->ipsclass->admin->error("Upload has failed");
				}
				
				$save = array();
				$save['linked_id'] = 0;
				$save['filename'] = $upload->original_file_name;
				$save['filesize'] = $_FILES['FILE_UPLOAD']['size'];
				$save['location'] = $upload->saved_upload_name;
			}
		}
		else
		{
			if($this->ipsclass->cache['market_items'][$input['linked_id']]['filename'] != $this->item['filename'] || $this->ipsclass->market_class->items_loaded[$input['linked_id']]->item_config['linked_id'] != 0)
			{
				$this->ipsclass->admin->error("Invalid item choosen");
			}
			
			if($this->item_config['location'] && file_exists($this->item_config['location']))
			{
				@unlink( $this->item_config['location'] );
			}
			
			$save['linked_id'] = $input['linked_id'];
			$save['filename'] = "";
			$save['filesize'] = "";
			$save['location'] = "";
		}
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->delete_items($inventory_ids);
		$inv_id = reset($inventory_ids);
		
		if($this->item_config['linked_id'])
		{
			$config = $this->ipsclass->market_class->items_loaded[$this->item_config['linked_id']]->item_config;
		}
		else
		{
			$config = $this->item_config;
		}
		
		$this->_write_log($inv_id, "Download file: ".$config['filename']);
		
		header('Content-Type: unknown/unknown');
		header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
		header('Content-Disposition: attachment; filename="' . $config['filename'] . '"');
		header('Content-Length: ' . $config['filesize'] );
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		
		$fp = fopen($config['location'],"rb");
		fpassthru($fp);
		fclose($fp);
		flush();
		
		exit();
	}
}
?>